package com.baomidou.springboot.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.springboot.common.SortEnum;
import com.baomidou.springboot.entity.File;
import com.baomidou.springboot.mapper.FileMapper;
import com.baomidou.springboot.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 关于我们 服务实现类
 * </p>
 *
 * @author QQQ
 * @since 2018-06-19
 */
@Service
public class FileServiceImpl implements FileService {

    @Autowired
    private FileMapper fileMapper;

    @Override
    public void save(File file) {
        file.setCreateDate(new Date());
        fileMapper.insert(file);
    }

    @Override
    public void delete(String id) {
        fileMapper.deleteById(id);
    }

    @Override
    public File edit(String id) {
        return fileMapper.selectById(id);
    }

    @Override
    public void update(File file) {
        file.setModifyDate(new Date());
        fileMapper.updateById(file);
    }

    @Override
    public File view(String id) {
        return fileMapper.selectById(id);
    }

    @Override
    public Page<File> list(Page<File> page, String search) {
        if (0 == page.getSize()) {
            page.setSize(10);
        }
        if (0 == page.getCurrent()) {
            page.setCurrent(1);
        }
        List<File> fileList = fileMapper.findByDateDesc(page, search);
        page.setRecords(fileList);
        return page;
    }

    @Override
    public void deleteBatch(List<String> ids) {
        if (null == ids) {
            return;
        }
        fileMapper.deleteBatchIds(ids);
    }

    @Override
    public List<File> findBySort(SortEnum sortEnum) {
        return fileMapper.findBySort(sortEnum.name());
    }

    @Override
    public List<File> findByLimit(Integer limit) {
        return fileMapper.findByLimit(limit);
    }

    @Override
    public List<File> findByMenuAndLimit(String menu, Integer limit) {
        return fileMapper.findByMenuAndLimit(menu, limit);
    }

    @Override
    public File findById(String id) {
        return fileMapper.selectById(id);
    }

    @Override
    public Page<File> findPage(Page<File> page, String menu) {
        if (0 == page.getSize()) {
            page.setSize(10);
        }
        if (0 == page.getCurrent()) {
            page.setCurrent(1);
        }
        List<File> fileList = fileMapper.findPage(page, menu);
        page.setRecords(fileList);
        return page;
    }
}
